﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Cards.Util;
using Cards.Objects;

namespace Cards.GUI
{
    public partial class MainFrame : Form
    {
        public static INS_Stage m_insState;

        public static FrmCreateMultiGame m_frmMultiGame;
        //public static FrmCreateMultiGame m_frmMultiGameServer;
        //public static FrmCreateMultiGame m_frmMultiGameClient;

        public static String sCurDir = "";
        public static Boolean sPlayNextState = false;
        public static CARD_PLAYING_KIND s_gameKind = CARD_PLAYING_KIND.BLACKJACK;
        public static int s_numOfPlayers = 0;
        public static Boolean sFirstPlayInStage = true;
        public static MULTI_PLAYER_TYPE s_multiType;
        public static string s_loginName;
        public static Boolean s_isServer = false;
        
        
        
        public MainFrame()
        {
            InitializeComponent();
            InitVariable();
            //CreateNew();
        }

        public void InitVariable()
        {
            sCurDir = Environment.CurrentDirectory;
            this.Size = new Size(new Point(Constants.s_tableWidth,
                                           Constants.s_tableHeight + menuStrip1.Height));
            this.Location = new Point(0, 0);
            
            s_numOfPlayers = 4;
            //s_gameKind = CARD_PLAYING_KIND.BLACKJACK;
            s_gameKind = CARD_PLAYING_KIND.VNPOCKER;

            vnPockerToolStripMenuItem.Checked = false;
            blackjackToolStripMenuItem.Checked = false;
            catteToolStripMenuItem.Checked = false;

            MainFrame.sFirstPlayInStage = true;
            m_frmMultiGame = null;
            m_insState = null;

            MainFrame.s_numOfPlayers = Constants.VNPOCKER_MAX_PLAYERS;
            MainFrame.s_gameKind = CARD_PLAYING_KIND.VNPOCKER;
            MainFrame.s_multiType = MULTI_PLAYER_TYPE.SINGLE_PLAYER;
            MainFrame.s_loginName = Constants.DEFAULT_PLAYER_NAME;
        }

        void CheckedMenuItemVNPocker()
        {
            vnPockerToolStripMenuItem.Checked = true;
            blackjackToolStripMenuItem.Checked = false;
            catteToolStripMenuItem.Checked = false;
        }

        void CheckedMenuItemBlackJack()
        {
            vnPockerToolStripMenuItem.Checked = false;
            blackjackToolStripMenuItem.Checked = true;
            catteToolStripMenuItem.Checked = false;
        }

        void CheckedMenuItemCatte()
        {
            vnPockerToolStripMenuItem.Checked = false;
            blackjackToolStripMenuItem.Checked = false;
            catteToolStripMenuItem.Checked = true;
        }

        public void Init()
        {
            s_isServer = false;
            switch (s_gameKind)
            {
                case CARD_PLAYING_KIND.VNPOCKER:
                    m_insState = new INS_VNPockerStage(1, 1, Constants.s_tableWidth, Constants.s_tableHeight, this, s_numOfPlayers);
                    this.Controls.Add(m_insState);
                    CheckedMenuItemVNPocker();
                    break;

                case CARD_PLAYING_KIND.BLACKJACK:
                    m_insState = new INS_BlackJackStage(1, 1, Constants.s_tableWidth, Constants.s_tableHeight, this, s_numOfPlayers);
                    this.Controls.Add(m_insState);
                    CheckedMenuItemBlackJack();
                    break;

                case CARD_PLAYING_KIND.CATTE:
                    m_insState = new INS_CatteStage();
                    this.Controls.Add(m_insState);
                    CheckedMenuItemCatte();
                    break;

                default:
                    break;
            }
        }

        public void InitMultiGame()
        {
            switch (s_gameKind)
            {
                case CARD_PLAYING_KIND.VNPOCKER:
                    m_insState = new INS_VNPockerStage(1, 1, Constants.s_tableWidth, Constants.s_tableHeight, this, s_numOfPlayers, true);
                    this.Controls.Add(m_insState);
                    CheckedMenuItemVNPocker();
                    break;

                case CARD_PLAYING_KIND.BLACKJACK:
                    m_insState = new INS_BlackJackStage(1, 1, Constants.s_tableWidth, Constants.s_tableHeight, this, s_numOfPlayers);
                    this.Controls.Add(m_insState);
                    CheckedMenuItemBlackJack();
                    break;

                case CARD_PLAYING_KIND.CATTE:
                    m_insState = new INS_CatteStage();
                    this.Controls.Add(m_insState);
                    CheckedMenuItemCatte();
                    break;

                default:
                    break;
            }
        }

        private void createNewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateNew();
        }

        public void CreateNew()
        {
            sFirstPlayInStage = true;
            sPlayNextState = false;
            if (m_insState != null)
            {
                m_insState.DeleteStage();
                this.Controls.Remove(m_insState);
                m_insState.Dispose();
            }
            Init();
            m_insState.BeginToPlay();
        }

        public void ContinueToPlay()
        {
            sPlayNextState = true;
            m_insState.Stop();
            m_insState.ContinueToPlay();
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            ContinueToPlay();
        }

        private void vnPockerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (s_gameKind != CARD_PLAYING_KIND.VNPOCKER)
            {
                s_gameKind = CARD_PLAYING_KIND.VNPOCKER;
                CheckedMenuItemVNPocker();
                s_numOfPlayers = 4;
                CreateNew();
            }
        }

        private void blackJackToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (s_gameKind != CARD_PLAYING_KIND.BLACKJACK)
            {
                s_gameKind = CARD_PLAYING_KIND.BLACKJACK;
                CheckedMenuItemBlackJack();
                s_numOfPlayers = 10;
                CreateNew();
            }
        }

        private void catteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (s_gameKind != CARD_PLAYING_KIND.CATTE)
            {
                s_gameKind = CARD_PLAYING_KIND.CATTE;
                CheckedMenuItemCatte();
                s_numOfPlayers = 4;
                CreateNew();
            }
        }
        
        private void singlePlayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UserInfo dlg = new UserInfo();

            dlg.PLAYERNAME = MainFrame.s_loginName;
            dlg.NUMPLAYER = MainFrame.s_numOfPlayers;
            dlg.GAMEKIND = MainFrame.s_gameKind;
            dlg.MULTITYPE = MULTI_PLAYER_TYPE.SINGLE_PLAYER;

            switch (dlg.ShowDialog())
            {
                case DialogResult.OK:
                    if (m_insState != null)
                    {
                        DlgMsgBox msgbox = new DlgMsgBox();
                        msgbox.MESSAGETEXT = "Do you want to quit current game?";
                        if (msgbox.ShowDialog() == DialogResult.Cancel)
                        {
                            return;
                        }
                    }
                    CreateNew();
                    break;

                case DialogResult.Cancel:
                default:
                    break;
            }
        }

        private void createGameServerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UserInfo dlg = new UserInfo();

            dlg.PLAYERNAME = MainFrame.s_loginName;
            dlg.NUMPLAYER = MainFrame.s_numOfPlayers;
            dlg.GAMEKIND = MainFrame.s_gameKind;
            dlg.MULTITYPE = MULTI_PLAYER_TYPE.MULTI_PLAYER_SERVER;

            switch (dlg.ShowDialog())
            {
                case DialogResult.OK:
                    if (m_frmMultiGame != null)
                    {
                        DlgMsgBox msgbox = new DlgMsgBox();
                        msgbox.MESSAGETEXT = "Do you want to quit current game?";
                        if (msgbox.ShowDialog() == DialogResult.Cancel)
                        {
                            return;
                        }
                    }

                    if (m_frmMultiGame != null)
                    {
                        m_frmMultiGame.ResetControlsState();
                        m_frmMultiGame.Dispose();
                        m_frmMultiGame = null;
                    }

                    m_frmMultiGame = new FrmCreateMultiGame(MULTI_PLAYER_TYPE.MULTI_PLAYER_SERVER);
                    m_frmMultiGame.ShowDialog(this);

                    break;

                case DialogResult.Cancel:
                default:
                    break;
            }
        }

        private void connectGameServerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UserInfo dlg = new UserInfo();

            dlg.PLAYERNAME = MainFrame.s_loginName;
            dlg.NUMPLAYER = MainFrame.s_numOfPlayers;
            dlg.GAMEKIND = MainFrame.s_gameKind;
            dlg.MULTITYPE = MULTI_PLAYER_TYPE.MULTI_PLAYER_CLIENT;

            switch (dlg.ShowDialog())
            {
                case DialogResult.OK:
                    if (m_frmMultiGame != null)
                    {
                        DlgMsgBox msgbox = new DlgMsgBox();
                        msgbox.MESSAGETEXT = "Do you want to quit current game?";
                        if (msgbox.ShowDialog() == DialogResult.Cancel)
                        {
                            return;
                        }
                    }

                    if (m_frmMultiGame != null)
                    {
                        m_frmMultiGame.ResetControlsState();
                        m_frmMultiGame.Dispose();
                        m_frmMultiGame = null;
                    }

                    m_frmMultiGame = new FrmCreateMultiGame(MULTI_PLAYER_TYPE.MULTI_PLAYER_CLIENT);
                    m_frmMultiGame.ShowDialog(this);

                    break;

                case DialogResult.Cancel:
                default:
                    break;
            }
        }

        private void MainFrame_Shown(object sender, EventArgs e)
        {
            UserInfo dlg = new UserInfo();

            dlg.PLAYERNAME = MainFrame.s_loginName;
            dlg.NUMPLAYER = MainFrame.s_numOfPlayers;
            dlg.GAMEKIND = MainFrame.s_gameKind;
            dlg.MULTITYPE = MULTI_PLAYER_TYPE.SINGLE_PLAYER;

            switch (dlg.ShowDialog())
            {
                case System.Windows.Forms.DialogResult.OK:
                    s_numOfPlayers = dlg.NUMPLAYER;
                    s_gameKind = dlg.GAMEKIND;
                    s_multiType = dlg.MULTITYPE;
                    s_loginName = dlg.PLAYERNAME;

                    switch (MainFrame.s_multiType)
                    {
                        case MULTI_PLAYER_TYPE.SINGLE_PLAYER:
                            CreateNew();
                            break;

                        case MULTI_PLAYER_TYPE.MULTI_PLAYER_SERVER:
                            m_frmMultiGame = new FrmCreateMultiGame(MULTI_PLAYER_TYPE.MULTI_PLAYER_SERVER);
                            m_frmMultiGame.ShowDialog(this);

                            if (m_frmMultiGame.ISSTARTEDGAME)
                            {
                                CreateNewMultiGame();
                            }

                            break;

                        case MULTI_PLAYER_TYPE.MULTI_PLAYER_CLIENT:
                            m_frmMultiGame = new FrmCreateMultiGame(MULTI_PLAYER_TYPE.MULTI_PLAYER_CLIENT);
                            m_frmMultiGame.ShowDialog(this);

                            if (m_frmMultiGame.ISSTARTEDGAME)
                            {
                                CreateNewMultiGame();
                            }

                            break;

                        default:
                            break;
                    }

                    break;

                case System.Windows.Forms.DialogResult.Cancel:
                default:
                    break;
            }

            dlg.Dispose();
        }

        public void CreateNewMultiGame()
        {
            sFirstPlayInStage = true;
            sPlayNextState = false;

            if (m_insState != null)
            {
                m_insState.DeleteStage();
                this.Controls.Remove(m_insState);
                m_insState.Dispose();
            }

            InitMultiGame();

            m_insState.BeginNetworkPlaying();
        }
    }
}
